package com.kclm.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;

/**
 * @Author : lvhx
 * @Date: 2025/11/4 15:30
 * @Description:
 */
//@Component("myDataSource02")
public class MyDataSource  implements DataSource {
//    @Value("${jdbc.url}")
    private String url;
//    @Value("${jdbc.driver}")
    private String driver;
//    @Value("${jdbc.username}")
    private String  username;
//    @Value("${jdbc.password}")
    private String password;

    public void setUrl(String url) {
        this.url = url;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public Connection getConnection() throws SQLException {
       try{
           //注册驱动
           Class.forName(driver);
           //获取连接
           Connection connection = DriverManager.getConnection(url,username,password);
           return   connection;
       } catch (ClassNotFoundException e) {
           throw new RuntimeException(e);
       }
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return null;
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {

    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {

    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }
}
